package com.ljx.recruitment.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.recruitment.common.domain.vo.resp.PageResp;
import com.ljx.recruitment.common.enums.CommonStatusTypeEnum;
import com.ljx.recruitment.user.domain.dto.TenantMenuDto;
import com.ljx.recruitment.user.domain.entity.Menu;
import com.ljx.recruitment.user.domain.entity.Permission;
import com.ljx.recruitment.user.domain.vo.req.MenuPageReq;
import com.ljx.recruitment.user.domain.vo.req.MenuSubmitReq;
import com.ljx.recruitment.user.domain.vo.req.MenuToRoleReq;
import com.ljx.recruitment.user.enums.TenantMenuEnum;
import com.ljx.recruitment.user.mapper.MenuMapper;
import com.ljx.recruitment.user.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-31  14:24
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Long> getMenuIdByRoleIds(List<Long> Menus) {
        return menuMapper.getMenuIdByRoleIds(Menus);
    }

    @Override
    public List<Menu> getMenuByIds(List<Long> menuIds) {
        if (menuIds.isEmpty()) {
            return new ArrayList<>();
        }
        return listByIds(menuIds);
    }

    @Override
    public List<TenantMenuDto> init() {
        return Arrays.stream(TenantMenuEnum.values())
                .map(item -> BeanUtil.copyProperties(item, TenantMenuDto.class))
                .collect(Collectors.toList());

    }

    @Override
    public PageResp<Menu> getPage(MenuPageReq req) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(req.getKey())){
            lambdaQueryWrapper.like(Menu::getName, req.getKey());
        }
        Page<Menu> page = menuMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);
        return PageResp.<Menu>builder()
                .total(page.getTotal())
                .list(page.getRecords())
                .build();
    }

    @Override
    public Menu submit(MenuSubmitReq req) {
        Menu menu;
        if (Objects.isNull(req.getId())){
            menu = BeanUtil.copyProperties(req, Menu.class);
            menuMapper.insert(menu);
        } else {
            menu = menuMapper.selectById(req.getId());
            BeanUtils.copyProperties(req, menu);
            menuMapper.updateById(menu);
        }

        return menu;
    }

    @Override
    @Transactional
    public void allocationToRole(MenuToRoleReq req) {
        menuMapper.deleteRoleMenu(req.getRoleId());
        if (CollUtil.isEmpty(req.getMenuIds())){
            return;
        }
        menuMapper.allocationToRole(req.getRoleId(), req.getMenuIds());
    }

    @Override
    public List<Menu> getMenuByCodes(List<String> menuCodes) {
        if (CollectionUtil.isEmpty(menuCodes)) {
            return new ArrayList<>();
        }
        return menuMapper.selectList(new LambdaQueryWrapper<Menu>()
                .in(Menu::getCode, menuCodes));
    }
}
